package fl;

import static java.lang.System.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author moroz
 */
public class WordSearcher {

    public static enum SEARCH_METHOD {

        SIMPLE
    }

    public static class Word {

        String core;
        ArrayList<String> suffixs;
        ArrayList<String> prefixs;

        public Word(String core) {
            this.core = core;
            this.suffixs = new ArrayList<String>(12);
            this.prefixs = new ArrayList<String>(12);
        }

        public void addSuffix(String end) {
            this.suffixs.add(end);
        }

        public void addPrefix(String end) {
            this.prefixs.add(end);
        }

        public static void buildCommaStr(ArrayList<String> list, StringBuilder b) {
            if (!list.isEmpty()) {
                for (String e : list) {
                    b.append(e);
                    b.append(',');
                }
                b.deleteCharAt(b.length() - 1); //remove last comma
            }
        }

        @Override
        public String toString() {
            StringBuilder buf = new StringBuilder("Word{ + core=[" + core + "], siffixs=[");
            buildCommaStr(suffixs, buf);
            buf.append("],prefixs[");
            buildCommaStr(prefixs, buf);
            buf.append("]");
            buf.append("}");
            return buf.toString();
        }
    }

    public static class Searcher {

        private SEARCH_METHOD method;

        public Searcher(SEARCH_METHOD method) {
            this.method = method;
        }

        private String concatAndClear(List<String> list) {
            StringBuilder buf = new StringBuilder();
            for (String line : list) {
                line = line.replaceAll("\\W+", " ").trim().toLowerCase();
                buf.append(line);
                buf.append(" ");
            }

            String result = buf.toString().trim();
            out.println(result);
            return result;
        }

        public ArrayList<Word> getWords(List<String> input) throws Exception {

            switch (method) {
                case SIMPLE:
                    return getWordsSimple(concatAndClear(input));
                default:
                    throw new Exception("unknown method");
            }



        }

        private ArrayList<Word> getWordsSimple(String strwords) {
            List<String> sortw = Arrays.asList(strwords.split(" "));
            Collections.sort(sortw);
            ArrayList<Word> words = new ArrayList<Word>(sortw.size());
            String c, next, pre = null;
            for (int i = 0; i < sortw.size(); i++) {
                c = sortw.get(i);
                if (c.length() >= 3) {
                    Word W = null;
                    for (int j = i + 1; j < sortw.size(); j++) {
                        next = sortw.get(j);

                        if (next.startsWith(c)) {
                            if (next.length() > c.length()) {
                                if (W == null) {
                                    W = new Word(c);
                                    words.add(W);
                                }
                                if (!next.equals(pre)) {
                                    W.addSuffix(next.substring(c.length()));
                                }
                                pre = next;
                            }
                            i = j;
                        } else {
                            break;
                        }
                    }

                }
            }
            return words;
        }
    }

    public static void main(String[] args) throws Exception {
        List<String> input = Arrays.asList(
                "There was 03453985 &&^%^%5 a long hard time..",
                " There was hardest time …",
                " it was one of the longest weeks ever…",
                "bra brahma brahmapurt brather barmin browser",
                "jknkcs 8q73458-0*** олтлот brom br abr bro"
                );

        Searcher s = new Searcher(SEARCH_METHOD.SIMPLE);
        Long t = System.nanoTime();
        out.println("current ntime:" + t);
        ArrayList<Word> words = s.getWords(input);
        for (Word w : words) {
            out.println(w.toString());
        }

        out.println("getWords(SIMPLE) ntime:" + (System.nanoTime() - t));

    }
}
